package com.ejuornal.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.velocity.VelocityContext;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Page;
import com.ejuornal.bean.User;
import com.ejuornal.dao.UserDao;
import com.ejuornal.service.UserService;
import com.ejuornal.util.Constants;
import com.ejuornal.util.EmailSender;
import com.ejuornal.util.MD5;
import com.ejuornal.util.VelocityUtil;

@Component("userService")
public class UserServiceImpl implements UserService {
	private UserDao userDao;
     
	public boolean isExists(String userName) {
		return userDao.isExists(userName);
	}
	
	public void add(User user) {
	    //注册用户默认的角色是1.
	    String password = user.getPassword();
	    user.setPassword(MD5.MD5Encode(password));
		String sql = "INSERT INTO t_user(roleId, userName, password, email,registeDate) VALUES(?, ?, ?, ?,now())";
		userDao.add(
				sql,
				new Object[] {1, user.getUserName(),
						user.getPassword(), user.getEmail()});
	}

	public void resetPwd(User user, String newPwd) {
	    String password  = MD5.MD5Encode(newPwd);
		String sql = "UPDATE t_user SET password = ? WHERE userId = ?";
		userDao.update(sql, new Object[] { password, user.getUserId() });
	}

	public User validate(String email) {
		User user = userDao.findByEmail(email);
		
		return user;
	}

	public void delete(int... userId) {
		String sql = "DELETE FROM t_user WHERE userId = ?";
		userDao.batchDelete(sql, userId);
	}

	public User findById(int userId) {
		return userDao.findById(userId);
	}

	public Page<User> findByRoleId(int current_record, String url,
			int roleId) {
		String hsql = "WHERE roleId = ?";
		return userDao.find(current_record, url, hsql, new Object[]{ roleId });
	}

    public Page<User> findAll(int current_record, String url) {
    	String sql_1 = " SELECT COUNT(*) FROM t_user u WHERE u.roleId != 4  ORDER BY u.roleId ASC ";
    	String sql_2 = " SELECT u.* FROM t_user u WHERE u.roleId != 4 ORDER BY u.roleId ASC limit " + current_record + ", 10 ";
    	return userDao.find(current_record, url, sql_1, sql_2, null);
    }
    @Override
	public void updatePwd(User user) {
//        String password = user.getPassword();
//        user.setPassword(MD5.MD5Encode(password));
    	String  sql=" update t_user set  password = ? where userId = ? ";
		userDao.update(sql, new Object[]{				
				user.getPassword(),
				user.getUserId()
		});
		System.out.println("impl -------"+user.getPassword());
	}
    @Override
	public boolean isExitsByEmail(String email) {
		return userDao.isExistsByEmail(email);
	}
    
	public UserDao getUserDao() {
		return userDao;
	}

	@Resource(name = "userDao")
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

    public User findByUserName(String userName) {
        return userDao.findByUserName(userName);
    }

    public boolean update(User user) {
        return userDao.update(user);
    }
    @Override
	public void updateRole(User user) {
		String  sql=" update t_user set roleId=? where userId = ? ";
		userDao.update(sql, new Object[]{				
				user.getRoleId(),
				user.getUserId()
		});
	}
    

	@Override
	public List<User> getListUsersEdit(int ChiefUserId) {
		// TODO Auto-generated method stub
		String  sql ="SELECT DISTINCT u.*   FROM t_editor editor ,t_periodical  per , t_user  u ,t_editor_chief  ec WHERE  "+ 
	            " ec.userId =per.userId AND per.periodicalId = editor.periodicalId   AND u.roleId = 5 AND editor.userId = u.userId AND ec.userId = ?";
		return userDao.getListUsers(ChiefUserId,sql);
	}

	@Override
	public List<User> getListUsersCommit(int ChiefUserId) {
		// TODO Auto-generated method stub
		String sql = "SELECT DISTINCT u.*   FROM t_editor_commit ecommit ,t_periodical  per , t_user  u ,t_editor_chief  ec WHERE  " + 
        " ec.userId =per.userId AND per.periodicalId = ecommit.periodicalId   AND  ecommit.userId = u.userId AND ec.userId = ?";
		return userDao.getListUsers(ChiefUserId, sql);
	}

	@Override
	public User findByEmail(String email) {
		System.out.println("ser email"+email);
		return userDao.findByEmail(email);
	}

	@Override
	public Page<User> findByEmail(int current_record, String url, String email) {
		// TODO Auto-generated method stub
		String hsql = "WHERE email = ?";
		return userDao.find(current_record, url, hsql, new Object[]{ email });
	}

	@Override
	public User getUserByEmail(String email) {
		// TODO Auto-generated method stub
		 return userDao.findByEmail(email);
	}

	@Override
	public User getUser(int articalId) {
		// TODO Auto-generated method stub
		String sql=" SELECT u.* FROM t_artical AS a INNER JOIN t_user AS u " + 
				" ON a.userId = u.userId WHERE a.articalId = ?";
		return userDao.getUser(articalId, sql);
	}

	@Override
	public void updateRoleIdOnly(User user) {
		userDao.updateByUserId(user);
	}

}
